﻿/*****************************************************************************
 * Copyright (C) 2009 Gwathuirim
 *
 * This is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 *
 * This is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Public License for more details
 *
 * To obtain a copy of the GNU General Public License, please see
 * <http://www.gnu.org.licenses/>
 *****************************************************************************/

// ==UserScript==
// @name           Imperion - NPCTradeHelper
// @namespace      http://code.google.com/p/imperion-gm/
// @include        http://*.imperion.*/market/npcTrade/r1/*/r2/*/r3/*
// ==/UserScript==

(function() {

/* language entries */
var language = new Object();

/* German */
/* For the *.imperion.de servers */
language['de'] = new Object();
language['de']['optimize'] = "optimieren";

/* English */
/* For the *.imperion.org servers */
language['org'] = new Object();
language['org']['optimize'] = "optimize";

/* Determine TLD for language support */
var languageKey = window.location.hostname;
languageKey = languageKey.split(".");
languageKey = languageKey[languageKey.length - 1];

var currentLanguage = language[languageKey];

/* Make some page specific available */
var trade = unsafeWindow['trade'];

/**
 * This method gets the maximal optimized count for the given input values
 */
trade.getOptimizedCount = function() {
	var neededSum = 0;
	for (var i = 0; i < 3; i++) 
		neededSum += this.$input(i);

	return Math.floor(trade.stockSum / neededSum);
}

/**
 * This method optimizes the values in the input fields
 */
trade.optimize = function() {
	var possibleCount = this.getOptimizedCount();
	
	for (var i = 0; i < 3; i++) 
		this.$input(i, this.$input(i) * possibleCount);
		
	this.displayResult();
	this.updateOptimizeLink();
}

/**
 * This method updates the Optimize link
 */
trade.updateOptimizeLink = function() {
	var link = document.getElementById("npcTradeHelper_optimizeLink");
	link.innerHTML = currentLanguage['optimize'] + " ";
	
	var optimizedCount = trade.getOptimizedCount();
	if (optimizedCount <= 1 || isNaN(optimizedCount)) {
		link.setAttribute("style", "text-decoration: line-through;");
		link.setAttribute("href", "#");
	} else {
		link.setAttribute("style", "");
		link.setAttribute("href", "javascript:trade.optimize();");
		link.innerHTML += "(" + trade.getOptimizedCount() + ")"
	}
}

//Insert the optimize link
var tdElements = document.getElementsByTagName("td");
for(var i = 0; i < tdElements.length; i++) {
	if (tdElements[i].getAttribute("class") == "p13 borderNone") {
		tdElements[i].innerHTML += "<br /><a id=\"npcTradeHelper_optimizeLink\"></a>";
		break;
	}
}

trade.updateOptimizeLink();

//Register change events
function registerChangeEvents(element) {
	element.addEventListener("change", trade.updateOptimizeLink, false);
	element.addEventListener("keyup", trade.updateOptimizeLink, false);
}
registerChangeEvents(document.getElementById("r1"));
registerChangeEvents(document.getElementById("r2"));
registerChangeEvents(document.getElementById("r3"));

})();